﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Imaging;

namespace pintu
{
  public  class SavepictureHelper
    {
       static string formmat = ".jpg";
        public async static Task<bool> SavepicwithUielement(UIElement ui)
        {
            try
            {
                string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
                var bitmap = new RenderTargetBitmap();
                StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync(filename + formmat,
                    CreationCollisionOption.GenerateUniqueName);
                await bitmap.RenderAsync(ui);
                var buffer = await bitmap.GetPixelsAsync();
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encod = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                    encod.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight,
                        DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, buffer.ToArray());
                    await encod.FlushAsync();
                    return true;

                }
            }catch(Exception ex)
            {
                return false;
            }

        }
        public static  async Task Copyimage(UIElement ui)
        {
            string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
            var bitmap = new RenderTargetBitmap();
            StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync(filename + formmat,
                CreationCollisionOption.GenerateUniqueName);
            await bitmap.RenderAsync(ui);
            var buffer = await bitmap.GetPixelsAsync();
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encod = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                encod.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight,
                    DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, buffer.ToArray());
                await encod.FlushAsync();
            }

             DataPackage dataPackage = new DataPackage();
        
            dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromFile(file));
            Clipboard.SetContent(dataPackage);

        }
        public async static Task<IRandomAccessStream> Buffer2RandomAccessStream(IBuffer buffer)
        {
            IRandomAccessStream randomStream = new InMemoryRandomAccessStream();
            DataWriter dataWriter = new DataWriter(randomStream);
            dataWriter.WriteBuffer(buffer, 0, buffer.Length);
            await dataWriter.StoreAsync();
            return randomStream;
        }
    }
}
